package com.moyun.zbmy.zizhou.ui.adapter.holder;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.gridsum.tracker.GridsumWebDissector;
import com.moyun.zbmy.zizhou.R;
import com.moyun.zbmy.zizhou.application.Constances;
import com.moyun.zbmy.zizhou.beans.AccountNewsItem;
import com.moyun.zbmy.zizhou.beans.ImageBean;
import com.moyun.zbmy.zizhou.beans.TopicContent;
import com.moyun.zbmy.zizhou.ui.adapter.AccountPicAdapter;
import com.moyun.zbmy.zizhou.ui.adapter.AttentionAdapter;
import com.moyun.zbmy.zizhou.ui.adapter.PicAdapter;
import com.moyun.zbmy.zizhou.ui.utils.GlideCircleTransform;
import com.moyun.zbmy.zizhou.ui.utils.GlideUtil;
import com.moyun.zbmy.zizhou.ui.utils.SkipUtil;
import com.moyun.zbmy.zizhou.ui.utils.UIUtils;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.DateUtils;
import com.ruihang.generalibrary.utils.ListUtils;
import com.ruihang.generalibrary.utils.UrlUtils;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.xutils.common.util.LogUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 互动 多图列表单元
 */
public class AttentionMoreImgNewsHolder extends BaseAbsHolder<AccountNewsItem> implements View.OnClickListener {

    @BindView(R.id.item_account_avatar)
    protected ImageView item_account_avatar;
    @BindView(R.id.item_account_name)
    protected CustomFontTextView item_account_name;
    @BindView(R.id.item_news_time)
    protected CustomFontTextView item_news_time;
    @BindView(R.id.to_subscribe)
    protected ImageView to_subscribe;

    @BindView(R.id.imgs_news_title)
    protected CustomFontTextView imgs_news_title;
    @BindView(R.id.imgs_layout)
    protected RelativeLayout imgs_layout;
    @BindView(R.id.item_pic_list)
    protected RecyclerView pic_list;

    @BindView(R.id.video_layout)
    protected RelativeLayout video_layout;
    @BindView(R.id.video_img)
    protected ImageView video_img;

    @BindView(R.id.attention_news_comment)
    protected CustomFontTextView attention_news_comment;
    @BindView(R.id.attention_news_digg)
    protected CustomFontTextView attention_news_digg;

    @BindView(R.id.item_comments_operation)
    protected LinearLayout item_comments_operation;
    @BindView(R.id.div_view)
    protected View div_view;

    private GridLayoutManager manager;
    private AccountPicAdapter adapter;


    private AttentionAdapter.OnAttentionNewsClickListener listener;
    private AccountNewsItem temp;
    private GlideCircleTransform circleTransform;
    private boolean isShowCommentLayout = true;

    public AttentionMoreImgNewsHolder(Context context, View itemView, AttentionAdapter.OnAttentionNewsClickListener listener) {
        super(context, itemView);
        itemView.setOnClickListener(this);
        this.listener = listener;
    }
    public AttentionMoreImgNewsHolder(Context context, View itemView, AttentionAdapter.OnAttentionNewsClickListener listener,boolean isShowCommentLayout ) {
        super(context, itemView);
        itemView.setOnClickListener(this);
        this.listener = listener;
        this.isShowCommentLayout = isShowCommentLayout;
    }
    public CustomFontTextView getAttentionNewsDigg() {
        return attention_news_digg;
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(AccountNewsItem t) {
        temp = t;
        //多图
        circleTransform = new GlideCircleTransform(context);
        GlideUtil.getCircleGlid(context, t.getFollowedSmallImage(), true).diskCacheStrategy(DiskCacheStrategy.NONE).transform(circleTransform).into(item_account_avatar);
        String timeStr = DateUtils.formatDate(DateUtils.getDataDate(t), "");
        item_news_time.setText(timeStr);
        item_account_name.setText(t.getFollowedName());
        imgs_news_title.setText(t.getNewsDetailText());
        if (t.isShowAttention()) {
            to_subscribe.setVisibility(View.VISIBLE);
        } else {
            to_subscribe.setVisibility(View.GONE);
        }
        if(isShowCommentLayout){
            item_comments_operation.setVisibility(View.VISIBLE);
            div_view.setVisibility(View.VISIBLE);
        }else{
            item_comments_operation.setVisibility(View.GONE);
            div_view.setVisibility(View.GONE);
        }
        if (t.getFollowedStatus() == 1) {
            to_subscribe.setImageDrawable(context.getResources().getDrawable(R.mipmap.icon_subscribed));
        } else {
            to_subscribe.setImageDrawable(context.getResources().getDrawable(R.mipmap.icon_subscribe));
        }
        if (ListUtils.isListValued(t.getNewsDetailImages())) {
            //多图
            imgs_layout.setVisibility(View.VISIBLE);
            video_layout.setVisibility(View.GONE);
            initView(t.getNewsDetailImages());
        } else if (t.getNewsDetailType() == 102) {
            video_layout.setVisibility(View.VISIBLE);
            imgs_layout.setVisibility(View.GONE);
            GlideUtil.getGlideWithSmall169DefCenterCrop(context, t.getDataImg()).into(video_img);
            video_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != temp) {
                        SkipUtil.skipToVideoPlay(context, UrlUtils.linkUrls(Constances.ST_BASE_URL, temp.getNewsDetailVideo()), "");
                    }
                }
            });
        } else {
            //无图
            imgs_layout.setVisibility(View.GONE);
            video_layout.setVisibility(View.GONE);
        }

        if (!t.isNewsDetailAttitudesStatus()) {
            Drawable drawable = context.getResources().getDrawable(R.mipmap.icon_compere_btn_normal);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            attention_news_digg.setDrawables(drawable, null, null, null);
            attention_news_digg.setTextColor(context.getResources().getColor(R.color.colorOpinionSelectNo));
        } else {
            Drawable drawable = context.getResources().getDrawable(R.mipmap.icon_compere_btn_press);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            attention_news_digg.setDrawables(drawable, null, null, null);
            attention_news_digg.setTextColor(context.getResources().getColor(R.color.colorTxRedMain_));
        }

        long upCount = t.getNewsDetailAttitudesCount();
        if (upCount > 0) {
            attention_news_digg.setText(String.valueOf(upCount));
        } else {
            attention_news_digg.setText("点赞");
        }

        long count = t.getNewsDetailCommentCount();
        if (count > 0) {
            attention_news_comment.setText(String.valueOf(count));
        } else {
            attention_news_comment.setText("评论");
        }
    }

    private void initView(List<ImageBean> dataImgs) {
        //  if (dataImgs.size() == 4) {
        //      manager = new GridLayoutManager(context, 2);
        //  } else {
        manager = new GridLayoutManager(context, 3);
        //  }

        pic_list.setItemAnimator(new DefaultItemAnimator());
//        ((NoAlphaItemAnimator)pic_list.getItemAnimator()).setSupportsChangeAnimations(false);
//        pic_list.getItemAnimator().setChangeDuration(0);// 通过设置动画执行时间为0来解决闪烁问题


        pic_list.setLayoutManager(manager);
        manager.setSmoothScrollbarEnabled(true);
        manager.setAutoMeasureEnabled(true);
        pic_list.setHasFixedSize(true);
        pic_list.setNestedScrollingEnabled(false);
        if (adapter == null) {
            // new UiListMarginUtils().setMargin(context, pic_list, R.dimen.item_list_large_margin_left, R.dimen.item_list_large_margin_right);
            adapter = new AccountPicAdapter(context, dataImgs);
        } else {
            adapter.setData(dataImgs);
        }
        pic_list.setAdapter(adapter);
    }

    @Override
    public void setItemMode(int mode) {
        super.setItemMode(mode);

    }

    @Override
    public void onDestory() {
        super.onDestory();
        if (itemView != null) {
            itemView.setOnClickListener(null);
        }
    }

    @Override
    public void onClick(View v) {
        if (temp == null) {
            Toast.makeText(context, "数据错误", Toast.LENGTH_SHORT).show();
            return;
        }
//        ArrayList<Pair<String, String>> pairs = new ArrayList<>();
//        pairs.add(new Pair<>("页面", "首页"));
//        pairs.add(new Pair<>("栏目", "精选"));
//        pairs.add(new Pair<>("位置", "24小时"));
//        ArrayList<Pair<String, String>> tempPair = new ArrayList<>();
//        tempPair.addAll(pairs);
//        tempPair.add(new Pair<>("行为类型", "点击"));
//
//        if (activity != null) {
//            GridsumWebDissector.getInstance().trackEvent(activity, "", temp.getNewsDetailTitle(), "", 200, tempPair);
//        } else if (fragment != null) {
//            GridsumWebDissector.getInstance().trackEvent(fragment, "", temp.getNewsDetailTitle(), "", 200, tempPair);
//        }
//        HashMap<String, String> map = new HashMap<>();
//        for (Pair<String, String> p : pairs) {
//            map.put(p.first, p.second);
//        }
        if (null != listener) {
            listener.OnItemClick(temp, AttentionAdapter.VIEWTYPE_NEWS_IMG_ITEM);
        }

    }

    @OnClick({R.id.attention_news_comment, R.id.attention_news_digg, R.id.to_subscribe, R.id.img_video_play})
    public void itemClick(View view) {
        switch (view.getId()) {
            case R.id.attention_news_comment:
//                if (listener != null) {
//                    listener.OnItemCommentClick(temp, getAdapterPosition());
//                }
                if (null != listener) {
                    listener.OnItemClick(temp, AttentionAdapter.VIEWTYPE_NEWS_IMG_ITEM);
                }
                break;
            case R.id.attention_news_digg:
                if (listener != null) {
                    listener.OnItemDiggClick(temp, getAdapterPosition());
                }
                break;
            case R.id.to_subscribe:
                if (listener != null) {
                    listener.OnItemAttentionClick(temp, getAdapterPosition());
                }
                break;
            case R.id.img_video_play:
                if (null != temp) {
//                    String mVideoCoverImage = UIUtils.getFirstFrameFromVideo(UrlUtils.linkUrls(Constances.ST_BASE_URL, temp.getNewsDetailVideo()));
                    SkipUtil.skipToVideoPlay(context, UrlUtils.linkUrls(Constances.ST_BASE_URL, temp.getNewsDetailVideo()), "");
                }
                break;

        }
    }

}
